// BlockUI.cs
using UnityEngine;
using TMPro;
using System.Collections;

public class BlockUI : MonoBehaviour
{
    [Header("组件引用")]
    public TextMeshPro blockText;
    public GameObject visualContainer; // 用于控制显示/隐藏的父物体

  
    private Vector3 originalPosition;
    private Vector3 originalScale;
    private int currentBlockValue = 0;

    private void Awake()
    {
        // 保存原始状态
        originalPosition = transform.position;
        originalScale = visualContainer.transform.localScale;

        // 初始隐藏
        if (visualContainer != null)
            visualContainer.SetActive(false);

        EffectSystem.OnBlockGained += OnBlockGained;

    }

    

    private void OnBlockGained(ITarget target, int blockValue)
    {
        // 只处理玩家的格挡值变化
        Debug.Log("玩家格挡值准备发生变化");
        if (target is PlayerData)
        {
            Debug.Log("玩家格挡值发生变化");
            UpdateBlockDisplay(blockValue);
        }
    }

    private void UpdateBlockDisplay(int newBlockValue)
    {
        currentBlockValue = newBlockValue;

        if (currentBlockValue > 0)
        {
            visualContainer.SetActive(true);
        }
        else
        {
            visualContainer.SetActive(false);
        }

        // 更新文本
        
        
        blockText.text = currentBlockValue.ToString();
        
    }

    

    private void OnDestroy()
    {
        // 清理事件订阅
        EffectSystem.OnBlockGained -= OnBlockGained;
    }
}